﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Wind.Utils;
using System.Text;
using System.Net;
using System.IO;
using Wind.Base;

namespace Wind.MultimediaStation.WebService
{
    public class RealHQHelper
    {
        private static string userid = "indexF9";// 请根据客户类型修改为 windiq, indexf9, bondservice 等
        private static List<List<string>> GetListFromString(string speedweb_quotestr, int count)
        {
            if (speedweb_quotestr == null
                || speedweb_quotestr == string.Empty
                || speedweb_quotestr.Substring(0, 5).ToLower().CompareTo("error") == 0)
                return null;
            string[] lines = speedweb_quotestr.Split(new string[] { "<br>" }, StringSplitOptions.RemoveEmptyEntries);
            List<List<string>> result = new List<List<string>>();
            for (int i = 0; i < lines.Length; i++)
            {
                if (count != 0 && i >= count)
                    break;
                string[] oneline = lines[i].Trim().Split(',');
                List<string> onelist = new List<string>();
                for (int j = 0; j < oneline.Length; j++)
                {
                    onelist.Add(oneline[j]);
                }
                result.Add(onelist);
            }
            return result;
        }
        public static List<List<string>> GetSpeedWebQuote(UInt32[] inds, string sectorname, UInt32 sortindicator, Char sortdirection, int count)
        {
            SpeedWebQuoteClient speedWebQuoteClient = new SpeedWebQuoteClient();
            string speedweb_quotestr = speedWebQuoteClient.GetSpeedWebQuoteSystemSector(inds, userid, sectorname, sortindicator, sortdirection);
            return GetListFromString(speedweb_quotestr, count);
        }
        public static List<List<string>> GetSpeedWebQuote(UInt32[] inds, string[] windcodes, UInt32 sortindicator, Char sortdirection, int count)
        {
            SpeedWebQuoteClient speedWebQuoteClient = new SpeedWebQuoteClient();
            string speedweb_quotestr = speedWebQuoteClient.GetSpeedWebQuoteUserSector(inds, userid, windcodes, sortindicator, sortdirection);
            return GetListFromString(speedweb_quotestr, count);
        }
    }
    public class SpeedWebQuoteClient
    {
        private string speedwebservername = string.Empty;
        public SpeedWebQuoteClient()
        {
            speedwebservername = ConfigurationSettings.AppSettings["SpeedWebServerName"];
        }
        public string GetSpeedWebQuoteSystemSector(UInt32[] inds, string userid, string systemsectorname, UInt32 sortindicator, Char sortdirection)
        {
            if (inds == null)
                return "";
            if (inds.Length == 0)
                return "";
            UInt32 sortind = 0;

            try
            {
                sortind = Convert.ToUInt32(sortindicator);
            }
            catch (Exception ex)
            {
                sortind = 0;
                LogHelper.Write(ex.Message);
            }

            string sortdirectionstr = "n";
            if ((sortind > 0) && (sortind < 384))
            {

                if ((sortdirection == 'D') || (sortdirection == 'd'))
                    sortdirectionstr = "d";
                else if ((sortdirection == 'A') || (sortdirection == 'a'))
                    sortdirectionstr = "a";
            }

            StringBuilder sb_inds = new StringBuilder();
            sb_inds.Append(inds[0]);
            for (int i = 1; i < inds.Length; i++)
            {
                sb_inds.Append(",");
                sb_inds.Append(inds[i].ToString());
            }
            string indicators = sb_inds.ToString();

            string url = "http://" + speedwebservername + "/SPEEDWEB/swintf.aspx?f=qs&b=8&m=5678&a=abcdefghi" +
                "&u=" + userid +
                "&s=" + systemsectorname +
                "&in=" + indicators +
                "&o=" + sortdirectionstr +
                "&si=" + sortind.ToString();

            WebClient client = new WebClient();
            try
            {
                Stream ms = client.OpenRead(url);
                StreamReader sr = new StreamReader(ms);
                return sr.ReadToEnd();
            }

            catch (Exception ex)
            {
                LogHelper.Write(ex.Message, LogMsgLevel.Error);
                return "";

            }
        }

        public string GetSpeedWebQuoteUserSector(UInt32[] inds, string userid, string[] windcodes, UInt32 sortindicator, Char sortdirection)
        {
            if ((inds == null) || (windcodes == null))
                return "";
            if ((inds.Length == 0) || (windcodes.Length == 0))
                return "";
            UInt32 sortind = 0;
            try
            {
                sortind = Convert.ToUInt32(sortindicator);
            }
            catch (Exception ex)
            {
                sortind = 0;
                LogHelper.Write(ex.Message);
            }

            string sortdirectionstr = "n";
            if ((sortind > 0) && (sortind < 384))
            {
                if ((sortdirection == 'D') || (sortdirection == 'd'))
                    sortdirectionstr = "d";
                else if ((sortdirection == 'A') || (sortdirection == 'a'))
                    sortdirectionstr = "a";
            }
            StringBuilder sb_inds = new StringBuilder();
            sb_inds.Append(inds[0]);
            for (int i = 1; i < inds.Length; i++)
            {
                sb_inds.Append(",");
                sb_inds.Append(inds[i].ToString());
            }
            string indicators = sb_inds.ToString();
            Int32 total_members = windcodes.Length;
            string sectorname = userid + "sector";

            string url = "http://" + speedwebservername + "/SPEEDWEB/swintf.aspx?f=qs&b=9&m=1234&a=abcdefghi" +
                "&u=" + userid +
                "&s=" + sectorname +
                "&in=" + indicators +
                "&o=" + sortdirectionstr +
                "&si=" + sortind.ToString();
            byte[] response = null;
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write(StopBitCodec.EncodeUInt32(Convert.ToUInt32(total_members)));
                    for (int i = 0; i < total_members; i++)
                        bw.Write(WindCodeCodec.Encode(windcodes[i]));

                    try
                    {
                        WebClient client = new WebClient();
                        response = client.UploadData(url, ms.ToArray());
                    }
                    catch (Exception ex)
                    {
                        return "error" + ex.Message;
                    }
                }
            }
            if (response != null)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < response.Length; i++)
                    sb.Append(Convert.ToChar(response[i]));

                return sb.ToString();
            }
            else
                return "";
        }
    }
}
